package com.fast.admin.common.utils;

import com.fast.admin.entity.sys.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

/**
 * shiro session工具类
 * 
 * @author ylc 2017年2月15日17:53:43
 *
 */
@Slf4j
public class ShiroUtil {
	/**
	 * 将一些数据放到ShiroSession中,以便于其它地方使用
	 * 
	 * @param key
	 * @param value
	 */
	public static void setSession(String key, Object value) {
		Subject currentUser = SecurityUtils.getSubject();
		if (null != currentUser) {
			Session session = currentUser.getSession();
			if (null != session) {
				session.setAttribute(key, value);
			}
		}
	}
	/**
	 * 获取session
	 * @param key
	 * @return
	 */
	public static Object getSession(String key) {
		Subject currentUser = SecurityUtils.getSubject();
		if (key != null) {
			Session session = currentUser.getSession();
			Object obj = session.getAttribute(key);
			return obj;
		}
		return null;
	}
	/**
	 * 获取当前用户
	 * @return Account
	 */
	public static User getCurrUser(){
        User user = null;
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		if(null != session){
			Object obj = session.getAttribute(Const.SYSTEM_USER);
			if(null != obj && obj instanceof User){
				try{
					user = (User) obj;
				}catch(Exception e){
                    log.error("获取用户失败");
				}
			}
		}
		return user;
	}
	/**
	 * 获取当前用户
	 * @return Account
	 */
	public static Long getCurrUserId(){
		return getCurrUser().getId();
	}
    public static Session getSession() {
        Subject subject= SecurityUtils.getSubject();
        Session session = subject.getSession();
        return session;
    }
}
